#!/bin/sh
APP_BASE=$HOME/workspaces/temp
APP_ETC=$APP_BASE/etc/

APP_NAME=services
APP_ROOT=$APP_BASE/boot
APP_BOOT=$APP_ROOT/$APP_NAME.jar
APP_RUN_PID=$APP_BASE/run/pid
APP_LOG=$APP_BASE/logs
APP_LOG_OUT=$APP_LOG/$APP_NAME.out

# 当确实需要主动回收内存以解决内存碎片问题时,可以考虑在业务空闲期间调用System.gc()
# -XX:+PrintFlagsFinal
# 需要震荡堆时使用(默认) -XX:MinHeapFreeRatio=40 -XX:MaxHeapFreeRatio=70
# -XX:NewRatio(CMS GC无效)
APP0_MEM_OPTS=" -Xmx1g -Xms1g -Xmn128m -XX:SurvivorRatio=2 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xss256k "
APP1_MEM_OPTS=" -Xmx800m -Xms800m -Xmn128m -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=80 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xss256k "
APP2_MEM_OPTS=" -Xmx1g -Xms1g -Xmn128m -XX:SurvivorRatio=2 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xss256k "
APP3_MEM_OPTS=" -Xmx2g -Xms2g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Xss256k "
COMMON_OPTS=" -server -XX:+PrintFlagsFinal -XX:MaxTenuringThreshold=15 -XX:+DisableExplicitGC "
# 已经默认设置 ‐XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+CMSClassUnloadingEnabled
CMS_OPTS=" -XX:+UseConcMarkSweepGC -XX:ParallelGCThreads=4 -XX:ConcGCThreads=4 -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSScavengeBeforeRemark -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+CMSClassUnloadingEnabled"
G1_OPTS=" -XX:+UseG1GC -XX:G1HeapRegionSize=1m -XX:MaxGCPauseMillis=200 -XX:TargetSurvivorRatio=50 -XX:InitiatingHeapOccupancyPercent=45 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=8 "
DUMP_OPTS=" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="$APP_LOG/$APP_NAME".dump"
# -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime
# PRINT_OPTS=" -XX:+DisableExplicitGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:-PrintGCTimeStamps -XX:+HeapDumpOnOutOfMemoryError "
PRINT_OPTS=" -XX:+DisableExplicitGC -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError "
APP_LOG_OPTS=" -Xlog:gc:"$APP_LOG/$APP_NAME".gc -XX:HeapDumpPath="$APP_LOG/$APP_NAME".dump"

APP0_OPTS=""
APP1_OPTS=""
APP2_OPTS=""
APP3_OPTS=""

JAVA_LIB=$JAVA_HOME/jre/lib/ext
DEBUG=true

clean_cache(){
    # 多个服务在一台机器上,暂不清理缓存
    echo "sync buffer to disk..."
    sync
    sleep 1
    echo "clean cache..."
    echo 3 > /proc/sys/vm/drop_caches
    sleep 1
}

init(){
  ## CMS_OPTS="$COMMON_OPTS $CMS_OPTS"
  CMS_OPTS="$COMMON_OPTS $G1_OPTS"
  G1_OPTS="$COMMON_OPTS $G1_OPTS"
  if $DEBUG == true; then
    APP0_OPTS="$CMS_OPTS $APP0_MEM_OPTS $PRINT_OPTS $APP_LOG_OPTS"
    APP1_OPTS="$CMS_OPTS $APP1_MEM_OPTS $PRINT_OPTS $APP_LOG_OPTS"
    APP2_OPTS="$CMS_OPTS $APP2_MEM_OPTS $PRINT_OPTS $APP_LOG_OPTS"
    APP3_OPTS="$G1_OPTS $APP3_MEM_OPTS $PRINT_OPTS $APP_LOG_OPTS"
  else
    APP0_OPTS="$CMS_OPTS $APP0_MEM_OPTS $DUMP_OPTS"
    APP1_OPTS="$CMS_OPTS $APP1_MEM_OPTS $DUMP_OPTS"
    APP2_OPTS="$CMS_OPTS $APP2_MEM_OPTS $DUMP_OPTS"
    APP3_OPTS="$G1_OPTS $APP3_MEM_OPTS $DUMP_OPTS"
  fi
}

start(){
    cd $APP_LOG
    rm -rf *.out
    #rm -rf *.log

    echo "start project begin..." >> $APP_LOG_OUT
    cd $APP_ROOT
    # nohup java $APP0_OPTS -Djava.ext.dirs=$JAVA_LIB -Djava.security.egd=file:/dev/./urandom -jar $APP_BOOT --spring.config.location=$APP_ETC >> $APP_LOG_OUT 2>&1 &
    nohup java $APP0_OPTS -Djava.security.egd=file:/dev/./urandom -jar $APP_BOOT --spring.config.location=$APP_ETC >> $APP_LOG_OUT 2>&1 &
    echo $! > $APP_RUN_PID
    echo "start project end..." >> $APP_LOG_OUT
}

stop(){
    echo "stop project..." >> $APP_LOG_OUT
    kill `cat $APP_RUN_PID`
    sleep 3
    rm -rf $APP_RUN_PID
    echo "stop project end..." >> $APP_LOG_OUT
}

restart(){
    stop
    sleep 1
    start
}

init
if [ "$1" = "start" ]; then
start
elif [ "$1" = "stop" ]; then
stop
elif [ "$1" = "restart" ]; then
restart
fi
